﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.TranferParam;
using ZLCMS.Web.SystemManage.Control;
using System.Collections.Generic;
using Newtonsoft.Json;
using NHibernate;
using DotNet.Common.TransferParam;
using DotNet.Common.NHibernateUtil;

namespace ZLCMS.Web.SystemManage.Basics
{
    public partial class TagEdit : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        public string _userAccount;
        protected ShopTagInfo _tagInfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化全局变量值
            InitializeGlobalValue();

            if (IsPostBack)
            {
                SaveUser();
            }
            else
            {
                ParamKit.Bind("ProductTagStatus", ddl_status, null, null, null, "", "1");
            }
  //初始化控件值
            InitializeContent();
        }

        #region 初始化全局变量 + protected void InitializeGlobalValue()
        /// <summary>
        /// 初始化全局变量
        /// </summary>
        protected void InitializeGlobalValue()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    if (!IsPostBack)
                    {
                        _id.Text = Request["uid"]??"-1";
                    }

                    _tagInfo = session.Get<ShopTagInfo>(int.Parse(_id.Text));
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }
        }
        #endregion

        #region 
        /// <summary>
        /// 初始化产品信息
        /// </summary>
        protected void InitializeContent()
        {
            //页面控件值
            if (!IsPostBack)
            {
                if (_tagInfo != null)
                {
                    tb_name.Text = _tagInfo.TagName;
                    tb_internal_name.Text = _tagInfo.TagInternalName;
                    txt_content.Value = _tagInfo.Content;
                    txt_remark.Value = _tagInfo.Remark;
                    tb_sort.Text = _tagInfo.Sort.ToString();
                    ddl_status.SelectedValue = _tagInfo.Status;
                    tb_sdate.Text = _tagInfo.BeginUseTime;
                    tb_edate.Text = _tagInfo.EndUseTime;
                    fc_open.Value = _tagInfo.PhotoUrl;

                    fc_open.Config = new Business.UploadFileInfo.UploadConfig()
                    {
                        Thumbnail = new ThumbnailConfig()
                        {
                            Mode = "HW",
                            Auto = true,
                            Name = ".thumb.jpg",
                            Height = 100,
                            Width = 100
                        },
                        UploadDir = _userAccount + "/product/"
                    };
                }
            }
        }
        #endregion

        #region 保存产品 + protected void SaveUser()
        /// <summary>
        /// 保存产品
        /// </summary>
        protected void SaveUser()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                if (_tagInfo==null)
                {
                    _tagInfo = new ShopTagInfo();
                    _tagInfo.OwnerAccount = "admin";
                }
                int count = Convert.ToInt32(session.CreateQuery("select count(*) from ShopTagInfo where TagName=:tname and Id!=:tid").SetParameter("tname", tb_name.Text).SetParameter("tid", _tagInfo.Id).UniqueResult());
                if (count > 0)
                {
                    _framePage.Alert(new ReturnCode()
                    {
                        statusCode = 300,
                        message = "标签名称已被占用!",
                        callbackType = "closeCurrent",
                        navTabId = _framePage.NavTabId
                    });
                    return;
                }
                count = Convert.ToInt32(session.CreateQuery("select count(*) from ShopTagInfo where TagInternalName=:tname and Id!=:tid").SetParameter("tname", tb_internal_name.Text).SetParameter("tid", _tagInfo.Id).UniqueResult());
                if (count > 0)
                {
                    _framePage.Alert(new ReturnCode()
                    {
                        statusCode = 300,
                        message = "标签内部名称已被占用!",
                        callbackType = "closeCurrent",
                        navTabId = _framePage.NavTabId
                    });
                    return;
                }
                _tagInfo.BeginUseTime = tb_sdate.Text;
                _tagInfo.EndUseTime = tb_edate.Text;
                _tagInfo.PhotoUrl = fc_open.Value;
                _tagInfo.Remark = txt_remark.Value;
                _tagInfo.Content = txt_content.Value;
                _tagInfo.Sort = int.Parse(string.IsNullOrEmpty(tb_sort.Text) ? "9999" : tb_sort.Text);
                _tagInfo.TagInternalName = tb_internal_name.Text;
                _tagInfo.TagName = tb_name.Text;
                _tagInfo.Status = ddl_status.SelectedValue;
                session.SaveOrUpdate(_tagInfo);
                session.Flush();

                _framePage.Alert(new ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功!",
                    callbackType = "closeCurrent",
                    navTabId = _framePage.NavTabId
                });
            }
        }
        #endregion

    }
}
